const state = {
  messages: [],
  messageTotal: 0
}

const mutations = {
  INIT_MESSAGE: (state, messages) => {
    state.messages = messages
  },
  ADD_MESSAGE: (state, message) => {
    state.messages.push(message)
  },
  DELETE_MESSAGE: (state) => {
    state.messages.splice(0)
  },
  SET_MESSAGE_TOTAL: (state, total) => {
    state.messageTotal = total
  }
}

const actions = {
  initMessage ({ commit }, messages) {
    commit('INIT_MESSAGE', messages)
  },
  addMessage ({ commit }, message) {
    commit('ADD_MESSAGE', message)
  },
  deleteMessage ({ commit }) {
    commit('DELETE_MESSAGE')
  },
  setMessageTotal ({ commit }, total) {
    commit('SET_MESSAGE_TOTAL', total)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
